package org.lex.ftplist.ui.action;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import org.dom4j.DocumentException;
import org.lex.collection.tree.ITreeNode;
import org.lex.ftplist.core.CacheService;
import org.lex.ftplist.core.Index;
import org.lex.ftplist.ui.ChooseCachedFtpDialog;
import org.lex.ftplist.ui.util.FtpTreeUtils;
import org.netbeans.swing.outline.Outline;

public class ViewCacheAction extends FLAction {

    public ViewCacheAction() {
        this.setName("View Cache");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Index index = ChooseCachedFtpDialog.showDialog();
        if (null != index) {
            try {
                ITreeNode root = CacheService.getInstance().load(index);
                Outline outline = FtpTreeUtils.createFtpListTree(root);
                JInternalFrame frame = new JInternalFrame("Tree", true, true, true, true);
                frame.setSize(400, 300);
                frame.setContentPane(new JScrollPane(outline));
                frame.setTitle(index.getUrl() + " cache");

                getOperator().addInternalFrame(frame);
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (DocumentException ex) {
                ex.printStackTrace();
            }
        }
    }
}
